package meijuli.com.join51.business.ui;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import meijuli.com.join51.business.R;
import meijuli.com.join51.business.SimpleAnimatorListener;
import meijuli.com.join51.business.utils.StringUtil;
import meijuli.com.join51.business.utils.LogUtils;

/**
 * 作者： Ryan
 * 描述：
 * 创建时间：2015/4/18 14:11.
 */
public class SearchView extends RelativeLayout implements View.OnClickListener {
    private static final String TAG = SearchView.class.getSimpleName();
    private OnSearchViewActionListener onSearchViewActionListener;
    private EditText searchInputView;
    private View mySearchView;
    private View editTextView;
    private TextView doneBtn;
    private View messageBtnContainer;
    private TextView messageCountView;

    public SearchView(Context context) {
        super(context);
        init();
    }

    public SearchView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public SearchView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public void setOnSearchViewActionListener(OnSearchViewActionListener actionListener) {
        onSearchViewActionListener = actionListener;
    }

    private void init() {
        mySearchView = inflate(getContext(), R.layout.my_search_view, this);
        searchInputView = (EditText) mySearchView.findViewById(R.id.searchInputView);
        searchInputView.setOnClickListener(this);
        editTextView = mySearchView.findViewById(R.id.editTextView);
        doneBtn = (TextView) mySearchView.findViewById(R.id.doneBtn);
        messageBtnContainer = mySearchView.findViewById(R.id.messageBtnContainer);
        messageCountView = (TextView) mySearchView.findViewById(R.id.messageCountView);
        final View cancelView = mySearchView.findViewById(R.id.cancelView);
        doneBtn.setOnClickListener(this);
        messageBtnContainer.setOnClickListener(this);
        cancelView.setOnClickListener(this);
        searchInputView.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if(!TextUtils.isEmpty(searchInputView.getText().toString())) {
                    doneBtn.setText("搜索");
                } else {
                    doneBtn.setText("取消");
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                String content = s.toString();
                if (TextUtils.isEmpty(content)) {
                    if (cancelView.getVisibility() == View.VISIBLE) {
                        AnimatorSet animatorSet = new AnimatorSet();
                        animatorSet.setDuration(500);
                        animatorSet.playTogether(ObjectAnimator.ofFloat(cancelView, "scaleX", 1f, 0f), ObjectAnimator.ofFloat(cancelView, "scaleY", 1f, 0f));
                        animatorSet.addListener(new SimpleAnimatorListener() {
                            @Override
                            public void onAnimationEnd(Animator animator) {
                                cancelView.setVisibility(View.INVISIBLE);
                                cancelView.setEnabled(false);
                                onSearchViewActionListener.onAction(OnSearchViewActionListener.ACTION_CLEAR_INPUT, "");
                                super.onAnimationEnd(animator);
                            }
                        });
                        animatorSet.start();
                        cancelView.setEnabled(false);
                    }
                } else {
                    if (cancelView.getVisibility() == View.INVISIBLE) {
                        cancelView.setEnabled(true);
                        cancelView.setVisibility(View.VISIBLE);
                        AnimatorSet animatorSet = new AnimatorSet();
                        animatorSet.setDuration(500);
                        cancelView.setVisibility(View.VISIBLE);
                        animatorSet.playTogether(ObjectAnimator.ofFloat(cancelView, "scaleX", 0f, 1f), ObjectAnimator.ofFloat(cancelView, "scaleY", 0f, 1f));
                        animatorSet.addListener(new SimpleAnimatorListener() {
                            @Override
                            public void onAnimationEnd(Animator animator) {
                                cancelView.setEnabled(true);
                                super.onAnimationEnd(animator);
                            }
                        });
                        animatorSet.start();
                    }
                }
            }
        });

        searchInputView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                LogUtils.i(TAG, "actionId = " + actionId);
                if (actionId == EditorInfo.IME_ACTION_SEARCH || actionId == 0 || actionId == EditorInfo.IME_ACTION_DONE || actionId == EditorInfo.IME_ACTION_GO) {
                    if (!StringUtil.isEmpty(v.getText().toString())) {
                        onSearchViewActionListener.onAction(OnSearchViewActionListener.ACTION_DONE, searchInputView.getText().toString());
                    }
                }
                return true;
            }
        });
    }

    /**
     * 设置搜索提示
     */
    public void setSearchHint(String hint) {
        if (null != searchInputView) {
            searchInputView.setHint(hint);
        }
    }

    public void setInputType(int inputType) {
        if (null != searchInputView) {
            searchInputView.setInputType(inputType);
        }
    }

    public void setMySearchViewEditBG(int editTextBgResID) {
        if (editTextBgResID != 0) {
            editTextView.setBackgroundResource(editTextBgResID);
        }
    }

    public void setDoneBtnVisibility(int visibility) {
        if (null != doneBtn) {
            doneBtn.setVisibility(visibility);
        }
    }

    public void setMessageBtnVisibility(int visibility) {
        if (null != messageBtnContainer) {
            messageBtnContainer.setVisibility(visibility);
        }
    }

    public void setMessageCountShow(int count) {
        if (null != messageCountView) {
            if (count == 0) {
                messageCountView.setVisibility(View.GONE);
                return;
            }
            if (count >= 10) {
                messageCountView.setText("9+");
                messageCountView.setVisibility(View.VISIBLE);
                return;
            }
            messageCountView.setText(count + "");
            messageCountView.setVisibility(View.VISIBLE);
        }
    }

    public void setEditTextSize(int dpSize) {
        if (null != searchInputView) {
            searchInputView.setTextSize(dpSize);
        }
    }

    public void setEditViewGravity(int gravity) {
        if (null != searchInputView) {
            searchInputView.setGravity(gravity);
        }
    }

    public String getInputText() {
        if (null != searchInputView) {
            return searchInputView.getText().toString();
        }
        return null;
    }

    public void setClearFocus() {
        if (null != searchInputView) {
            searchInputView.clearFocus();
            searchInputView.setFocusable(false);
        }
    }

    public void setSearchInputViewText(String inputContent) {
        if (null != searchInputView && !TextUtils.isEmpty(inputContent)) {
            searchInputView.setText(inputContent);
            searchInputView.setSelection(inputContent.length());
        }
    }

    @Override
    public void onClick(View view) {
        if (onSearchViewActionListener != null) {
            switch (view.getId()) {
                case R.id.searchInputView:
                    onSearchViewActionListener.onAction(OnSearchViewActionListener.ACTION_CLICK_SEARCH_INPUTVIEW, "");
                    break;
                case R.id.doneBtn:
                    //onSearchViewActionListener.onAction(OnSearchViewActionListener.ACTION_CLOSE, searchInputView.getText().toString());
                    onSearchViewActionListener.onAction(OnSearchViewActionListener.ACTION_DONE, searchInputView.getText().toString());
                    break;
                case R.id.cancelView:
                    searchInputView.setText("");
                    onSearchViewActionListener.onAction(OnSearchViewActionListener.ACTION_CLEAR_INPUT, "");
                    break;
                case R.id.messageBtnContainer:
                    onSearchViewActionListener.onAction(OnSearchViewActionListener.ACTIION_MESSAGE, "");
                    break;
            }
        }
    }

    public interface OnSearchViewActionListener {
        int ACTION_DONE = 1;
        int ACTION_CLEAR_INPUT = 2;
        int ACTION_CLOSE = 3;
        int ACTIION_MESSAGE = 4;
        int ACTION_CLICK_SEARCH_INPUTVIEW = 5;

        void onAction(int action, String result);
    }

}
